//
//  HIMainViewController.swift
//  Health_Infomation
//
//  Created by 林帆 on 2017/8/17.
//  Copyright © 2017年 林帆. All rights reserved.
//

import UIKit
import Alamofire

class HIMainViewController: HIRootViewController , UISearchBarDelegate , HIScrollMenuDelegate , HIMECollVDelegate{
 
    lazy var _SearchBar = UISearchBar(placeholder: "输入关键字搜索", imageName: "搜索")
    lazy var _ScrollMenu = HIScrollMenu(itemArray: ["推荐","膳食养生","母婴","运动健康","生活","明星"], frame: CGRect(x: 0, y: 0, width: kScreenWitdh, height: 44))
    lazy var _tableview = UITableView()
    var _idUsr : String = ""
    var _DataArray : [HealthInfoModel]?
    lazy var _HInfoVModel = HealthInfoViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        _NavBar.collectionBtnTitle = "我的收藏"
        ConfigSearBar()
        ConfigScrollMenu()
        ConfigTableView()
        LoadNetData()
    }
    
    func ConfigSearBar() {
        _SearchBar.delegate = self
        view.addSubview(_SearchBar)
        _SearchBar.snp.makeConstraints { (make) in
            make.top.equalTo(_NavBar.snp.bottom).offset(10)
            make.height.equalTo(33)
            make.width.equalTo(kScreenWitdh)
            make.left.equalTo(self.view)
        }
    }
    
    func ConfigScrollMenu() {
        _ScrollMenu.HIScrollDelegate = self
        view.addSubview(_ScrollMenu)
        _ScrollMenu.snp.makeConstraints { (make) in
            make.top.equalTo(_SearchBar.snp.bottom).offset(10)
            make.height.equalTo(44)
            make.width.equalTo(kScreenWitdh)
            make.left.equalTo(self.view)
        }
    }
    
    
    func HIScrollMenuDelegateDo() {
        let _HIMenuEditCollectionV =  HIMenuEditCollectionView(
            DataArray: [["推荐","膳食养生","母婴","运动健康","生活","明星"],
                        ["穿衣搭配","育儿","美食","夏季健康","冷知识","房价","极限挑战","娱乐资讯","共享单车","美食","育儿"]])
        _HIMenuEditCollectionV.alpha = 0
        _HIMenuEditCollectionV.HIMECollVDelegate = self
        _HIMenuEditCollectionV.backgroundColor = kRGBColorFromHex(rgbValue: 0xfafafa)
        self.view.addSubview(_HIMenuEditCollectionV)
        _HIMenuEditCollectionV.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(self._NavBar.snp.bottom)
        }
        UIView.animate(withDuration: 0.5, delay: 0, options: .layoutSubviews, animations: {
            _HIMenuEditCollectionV.alpha = 1
            if _HIMenuEditCollectionV.isEdited == true {
                _HIMenuEditCollectionV.isEdited = false
                _HIMenuEditCollectionV.MainCollectionView.reloadData()
            }
        }, completion: nil)
    }
    
    func HIMECollVbackToVC(dataArray: Array<String>) {
         _ScrollMenu.ReConfigItemBtn(dataArray: dataArray)
    }
    
    override func HINavigationPushVC() {
        let MyCollectionVC = HICollectionViewController()
        self.navigationController?.pushViewController(MyCollectionVC, animated: true)
    }
    
    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
        let vc = HISearchPageViewController()
        self.navigationController?.pushViewController(vc, animated: true)
        return false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

//tableview 相关
extension HIMainViewController : UITableViewDelegate , UITableViewDataSource {
    
    func ConfigTableView () {
        _tableview.delegate = self
        _tableview.dataSource = self
        _tableview.register(NormalTableViewCell.self, forCellReuseIdentifier: "NormalTableViewCell")
        view.addSubview(_tableview)
        _tableview.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(_ScrollMenu.snp.bottom)
        }
    }
    
    //MARK: Delegates
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NormalTableViewCell", for: indexPath) as! NormalTableViewCell
        cell.TitleLabel.text = "文章标题"
        cell.dateLabel.text = "今天天气不错"
        cell.MainImageView.image = UIImage(named: "插画")
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        NotificationCenter.default.post(name: NSNotification.Name("open"), object: nil)
    }
}

extension HIMainViewController {

    func LoadNetData() {
        let parametersDic = ["areaNo" : "bFgYKLHk" ,
                             "fgTemp" : "0" ,
                             "idAccount" : "12345678"];
        Alamofire.request("http://111.200.225.242:18086/hinfo/view/usract/init", method: .post, parameters: parametersDic, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
            
            if (response.error == nil){
                print("请求成功");
                MGLog(response)
                let data = response.result.value as! [String : AnyObject]
                self._idUsr = (data["data"] as? String)!
                self._HInfoVModel.loadHealthInfo(idUsr: self._idUsr)
            }else{
                print("请求失败");
            }
        }
    }
    func LoadMenuData() {
        }
}









